<?php

/**
 * 客户端调用接口
 */
class Mobile_UserController extends MM_Controller_Mobile_Action
{   
    //---------------------------------------------------
    // 公共接口
    //---------------------------------------------------
    
    /**
     * login
     */
    public function loginAction()
    {
    	if ( !isset($this->httpdata['email']) || empty($this->httpdata['email'])
    			|| !isset($this->httpdata['password']) || empty($this->httpdata['password']) ) {
    		$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Missing parameter: email...');
    	} else {
	    	$email = $this->httpdata['email'];
	    	$pwd = $this->httpdata['password'];
    		$this->result = $this->service->login($email, $pwd);
    	}
    	echo $this->encodeResult($this->result);
    }
    
    /**
     * register
     */
    public function registerAction()
    {
    	$imei = @$this->httpdata['imei'];
    	if ( !isset($imei) || empty($imei) ) {
    		$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Missing parameter: imei');
    	} else {
    		$nickname =  isset($this->httpdata['nickname'])?$this->httpdata['nickname']:'新用户';
    		
    		$this->result = $this->service->register($nickname,$imei);
    	}
    	echo $this->encodeResult($this->result);
    }
    
    /**
     * binding by email
     */
    public function bindingemailAction()
    {
    	if ( ($this->uid<1) 
    			|| !isset($this->httpdata['email']) || empty($this->httpdata['email'])
    			|| !isset($this->httpdata['password']) || empty($this->httpdata['password'])  ) {
    		$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Missing parameter: uid...');
    	} else {
    		$email = $this->httpdata['email'];
    		$pwd = $this->httpdata['password'];
    		$this->result = $this->service->bindingByEmail($this->uid, $email, $pwd);
    	}
    	echo $this->encodeResult($this->result);
    }
    
    /**
     * binding by mobile
     */
    public function bindingmobileAction()
    {
    	if ( ($this->uid<1)
    			|| !isset($this->httpdata['mobile']) || empty($this->httpdata['mobile'])  ) {
    		$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Missing parameter: uid...');
    	} else {
    		$mobile = $this->httpdata['mobile'];
    		$this->result = $this->service->bindingByMobile($this->uid, $mobile);
    	}
    	echo $this->encodeResult($this->result);
    }
    
    /**
     * upload user img
     */
    public function uploaduserimgAction()
    {
    	if ( ($this->uid<1) ) {
    		$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Missing parameter: uid');
    	} else {
    		$this->result = $this->service->uploadUserimg($this->uid);
    	}
    
    	echo $this->encodeResult($this->result);
    }
    
    /**
     * upload user space img
     */
    public function uploaduserspaceimgAction()
    {
    	if ( ($this->uid<1) ) {
    		$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Missing parameter: uid');
    	} else {
    		$this->result = $this->service->uploadUserSpaceImg($this->uid);
    	}
    
    	echo $this->encodeResult($this->result);
    }
    
    /**
     * update user info
     */
    public function updateuserinfoAction()
    {
    	if ( ($this->uid<1) ) {
    		$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Missing parameter: uid');
    	} else {
    		
    		$serviceUser = new Mediad2_Service_Admin_User();
    		$result = $serviceUser->find($this->uid);
    		if ($result->getCode()==$result::CODE_FAILURE) {
    			$this->result = $result;
    		} else {
    			$user = $result->getData();
    			if (isset($this->httpdata['nickname']))
    				$user->setNickname($this->httpdata['nickname']);
    			if (isset($this->httpdata['email']))
    				$user->setEmail($this->httpdata['email']);
    			if (isset($this->httpdata['mobile']))
    				$user->setMobile($this->httpdata['mobile']);
    			if (isset($this->httpdata['qq']))
    				$user->setQq($this->httpdata['qq']);
    			
    			$this->result = $serviceUser->update($user);
    		}
    	}
    	echo $this->encodeResult($this->result);
    }
    
    /**
     * get user info
     */
    public function getuserinfoAction()
    {
    	if ( ($this->uid<1) ) {
    		$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'缺少参数');
    	} else {
    		$this->result = $this->service->getUserInfo($this->uid);
    	}
    	echo $this->encodeResult($this->result);
    }
    
    /**
     * get user files
     */
    public function getuserfilesAction()
    {
    	if ( ($this->uid<1)
    			|| !isset($this->httpdata['addtime']) ) {
    		$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Missing parameter: uid/addtime');
    	} else {
    		$addtime= empty($this->httpdata['addtime'])?0:$this->httpdata['addtime'];
    		$needgroup = empty($this->httpdata['needgroup'])?0:$this->httpdata['needgroup'];
    		$this->result = $this->service->getUserFiles($this->uid, $addtime, $needgroup);
    	}
    	
    	echo $this->encodeResult($this->result);
    }
    
    //---------------------------------------------------
    // 私有方法
    //---------------------------------------------------
    
}
	
	

	